package tenants

import (
	"context"
	"product/common/validator"
	"strconv"

	"product/authorization/serve/internal/svc"
	"product/authorization/serve/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetTenantByNameLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 查询租户列表
func NewGetTenantByNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTenantByNameLogic {
	return &GetTenantByNameLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetTenantByNameLogic) GetTenantByName(req *types.GetTenantByNameReq) (resp *types.GetTenantByNameResp, err error) {
	if validator.IsEmptyString(req.Name) {
		return &types.GetTenantByNameResp{Ok: false, Message: "租户名称不能为空"}, nil
	}
	tenant, err := l.svcCtx.TenantModel.FindTenantByName(l.ctx, req.Name)
	if err != nil {
		return &types.GetTenantByNameResp{
			Message: "查询失败",
			Ok:      false,
		}, nil
	}

	return &types.GetTenantByNameResp{
		Ok:   true,
		Name: tenant.Name,
		Id:   strconv.FormatInt(tenant.ID, 10)}, nil
}
